package com.leyou.item.service.impl;

import com.leyou.common.enums.ItemExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.dto.CategoryDto;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.CategoryService;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/03 18:24
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryMapper categoryMapper;//通用mapper会在运行时动态创建接口的实现类，这里idea在编译期会提示无对象可以注入


	@Override
	public List<Category> queryParentCategory(Long pid) {

		Category record = new Category();
		record.setParentId(pid);
		List<Category> categories = categoryMapper.select(record);
		if (CollectionUtils.isEmpty(categories)) {
			throw new LyException(ItemExceptionEnum.CATEGORY_NOT_FOUND);
		}
		return categories;

	}

	@Override
	public List<Category> queryCategoriesByBrandId(Long id) {
		List<Category> list = categoryMapper.queryCategoriesByBrandId(id);
		if (CollectionUtils.isEmpty(list)) {
			throw new LyException(ItemExceptionEnum.CATEGORY_NOT_FOUND);
		}
		return list;

	}

	@Override
	public List<Category> queryCategoriesByCids(List<Long> categoryIds) {
		//selectByIdList需要categoryMapper继承一个SelectByIdListMapper<Category,Long>接口
		return categoryMapper.selectByIdList(categoryIds);
	}

	@Override
	public List<CategoryDto> queryCategoriesByCid3(Long cid3) {
		Category category3 = categoryMapper.selectByPrimaryKey(cid3);
		Category category2 = categoryMapper.selectByPrimaryKey(category3.getParentId());
		List<CategoryDto> categoryDtoList = BeanHelper.copyWithCollection(Arrays.asList(category2, category3), CategoryDto.class);
		return categoryDtoList;
	}
}
